home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / kh_gdi.zip / GENERAL / GEOM.H next >
C/C++ Source or Header  |  1995-10-03  |  6KB  |  282 lines

  1.  
  2.  
  3. #ifndef __GEOM
  4. #define __GEOM
  5. #define __GEOM_H
  6.  
  7. #define location loc
  8. #include <assert.h>
  9.  
  10.  
  11.  struct dimension     // District
  12.  
  13.  { int a,b;
  14.  
  15.    dimension()  { a=b=0;}
  16.    dimension(int length) {a=0;b=length-1;}
  17.    dimension(int a, int b)  { this->a=a; this->b=b; }
  18.  
  19.    void sort() // b >= a. If not sure - use sort.
  20.    {if (a>b) {register tmp=a;a=b;b=tmp;}}
  21.  
  22.    int l()const { return b-a+1;}
  23.  
  24.    void center(const dimension& stat)   
  25.    {register l1=l();
  26.     a=(stat.l()-l1)/2+stat.a; b=a+l1-1;
  27.     }
  28.    void center(int c)
  29.    { register l1=l();
  30.     a=c+ (a-b)/2; b=a+l1-1;
  31.     }
  32.  
  33.    int center(){return (a+b)/2;}
  34.  
  35.    int operator==(const dimension& x){return x.l()==l();}
  36.    int operator<=(const dimension& x){return l()<=x.l();}
  37.    int operator>=(const dimension& x){return l()>=x.l();}
  38.  
  39.    int operator>(const dimension& x){return l()>x.l();}
  40.    int operator<(const dimension& x){return l()<x.l();}
  41.  
  42.  
  43.   };
  44.  
  45.  
  46.  struct rect;
  47.  
  48.  struct loc
  49.  { int X,Y;
  50.  
  51.    loc(int x,int y): X(x),Y(y){}
  52.    loc(): X(0),Y(0){}
  53.    loc(const loc& L): X(L.X),Y(L.Y){}
  54.  
  55.  
  56.    loc& operator+=(const loc& a)
  57.      { X+=a.X;Y+=a.Y; return * this;}
  58.    loc& operator-=(const loc& a)
  59.      { X-=a.X;Y-=a.Y; return * this;}
  60.  
  61.    void zero() {X=Y=0;}
  62.  
  63.    rect corner(const loc& cornr);  // See inline expansion below
  64.  };
  65.  
  66.  inline loc operator+ (const loc& a, const loc& b)
  67.   {
  68.     return loc(a.X+b.X,a.Y+b.Y);
  69.   }
  70.  
  71.  inline loc operator- (const loc& a, const loc& b)
  72.   {
  73.     return loc(a.X-b.X,a.Y-b.Y);
  74.   }
  75.  
  76.  inline loc operator+ (const loc& a, int D )
  77.   {
  78.     return loc(a.X+D,a.Y+D);
  79.   }
  80.  
  81.  inline loc operator- (const loc& a, int D)
  82.   {
  83.     return loc(a.X-D,a.Y-D);
  84.   }
  85.  
  86.  
  87.  
  88.   struct row : dimension
  89.      { int Y;
  90.  
  91.        row(const loc& O,int length) : dimension(O.X,O.X+length-1){Y=O.Y;}
  92.        row(int x1,int x2,int y):dimension(x1,x2){Y=y;}
  93.        row(const dimension& d,int y)  : dimension(d){Y=y;}
  94.  
  95.      //
  96.      // + += - -=  operations with loc and int
  97.      //
  98.        row operator+ (const loc& vect)
  99.      {return row(a+vect.X,b+vect.X,Y+vect.Y);}
  100.        row operator- (const loc& vect)
  101.      {return row(a-vect.X,b-vect.X,Y-vect.Y);}
  102.  
  103.        row& operator+= (const loc& vect)
  104.      {a+=vect.X;b+=vect.X;Y+=vect.Y;return *this;}
  105.        row& operator-= (const loc& vect)
  106.      { a-=vect.X,b-=vect.X;Y-=vect.Y;return *this;}
  107.  
  108.        row operator+ (int delta)
  109.         { return row(*this,Y+delta);}
  110.        row operator- (int delta)
  111.         { return row(*this,Y-delta);}
  112.        row& operator+= (int delta)
  113.      {Y+=delta;return *this;}
  114.        row& operator-= (int delta)
  115.      { Y-=delta;return *this;}
  116.  
  117. // Move to new place
  118.        void moveto(const loc& newOrg)
  119.     { register l1=l();
  120.       a=newOrg.X;
  121.       b=a+l1-1;
  122.       Y=newOrg.Y;
  123.     }
  124.      };
  125.  
  126.  
  127.  
  128. //
  129. //
  130. //
  131.  
  132.  struct rect
  133.   { loc origin,corner;
  134.  
  135.     rect(const rect& R): origin(R.origin),corner(R.corner){}
  136.     rect(loc org, loc corn){origin=org; corner=corn;}
  137.     rect(int x1,int y1,int x2,int y2) : origin(x1,y1), corner(x2,y2){}
  138.     rect(const dimension& hor,const dimension& vert): origin(hor.a,vert.a),
  139.                        corner(hor.b,vert.b){}
  140.     rect() :origin(),corner(){}
  141.    // rect( loc ext) : origin(),corner(ext){}
  142.    //  This constructor is no more supported, use extent() function instead
  143.  
  144.     void sort() 
  145.      { register tmp;
  146.      if(origin.X>corner.X) {tmp=origin.X; origin.X=corner.X;corner.X=tmp;}
  147.      if(origin.Y>corner.Y) {tmp=origin.Y; origin.Y=corner.Y;corner.Y=tmp;}
  148.      }
  149.  
  150.     dimension horiz() const {return dimension(origin.X,corner.X);}
  151.     dimension vert() const {return dimension(origin.Y,corner.Y);}
  152.     int  width() const {return horiz().l();}
  153.     int  height() const {return vert().l();}
  154.     loc extent() const {return loc(horiz().l(),vert().l());}
  155.  
  156.     unsigned area() const {return width()*height();}
  157.  
  158.     int  top()    const {return origin.Y;}
  159.     int  left()   const {return origin.X;}
  160.     int  bottom() const {return corner.Y;}
  161.     int  right()  const {return corner.X;}
  162.  
  163.     loc center()  const 
  164.     {return loc((origin.X+corner.X)/2,(origin.Y+corner.Y)/2);}
  165.  
  166.     void moveto(const loc& New)
  167.     {corner+=New-origin;origin=New;}
  168.  
  169.  
  170.     rect& operator+=(const loc&  vect)
  171.     {origin+=vect; corner+=vect; return * this;}
  172.     rect& operator-=(const loc&  vect)
  173.     {origin-=vect; corner-=vect; return * this;}
  174.  
  175.     int contains(loc point) const
  176.      {return
  177.       origin.X<=point.X && origin.Y <= point.Y && 
  178.       corner.X>=point.X && corner.Y >= point.Y ;
  179.       }
  180.  
  181.      int contains( const rect& A) const
  182.      { return contains(A.origin)&& contains(A.corner); }
  183.  
  184.      void expand(const rect& delta) 
  185.      { origin-=delta.origin; corner+=delta.corner;}
  186.  
  187.      void symmexpand(loc delta)
  188.      { origin-=delta; corner+=delta;}
  189.  
  190.  
  191.   };
  192.  
  193.  
  194.  inline rect extent(loc org,loc ext)
  195.    {return rect(org,org+ext-loc(1,1)); }
  196.  
  197.  inline rect extent(loc ext)
  198.    {return rect(loc(),ext-loc(1,1)); }
  199.  
  200.  
  201.  inline rect operator+(const rect& r,  loc v)
  202.   {return rect(r.origin+v ,(r.corner+v));}
  203.  
  204.  inline rect operator-(const rect& r,loc v)
  205.   {return rect(r.origin-v ,(r.corner-v));}
  206.  
  207.  inline rect loc::corner(const loc& cornr)
  208.    { return rect(*this,cornr);}
  209.  
  210. /*
  211. #ifdef __STDLIB_H
  212. #undef max
  213. #undef abs
  214. #endif
  215.  
  216. inline int max( int a,int b)
  217.  {
  218.    return a>b ? a: b;
  219.  }
  220.  
  221. #ifndef __STDLIB_H
  222.  
  223. inline int abs(int x)
  224.  {
  225.   return x<0 ? -x : x;
  226.   }
  227. #endif
  228.  
  229. inline loc max(loc a, loc b)
  230.   {
  231.    return loc(max(a.X,b.X),max(a.Y,b.Y));
  232.   }
  233.  
  234.  
  235. inline loc abs(loc a)
  236.   {
  237.    return loc(abs(a.X),abs(a.Y));
  238.    }
  239.  
  240. */
  241. inline loc operator/(loc num,int den)
  242.  {
  243.   assert(den>0);
  244.   return loc(num.X/den,num.Y/den);
  245.  
  246.  }
  247.  
  248. inline rect operator/(const rect& num,int den)
  249.    {
  250.      return rect(num.origin/den,num.corner/den);
  251.  
  252.    }
  253.  
  254. inline loc operator*(loc m,int m2)
  255.  {
  256.   return loc(m.X*m2,m.Y*m2);
  257.  
  258.  }
  259.  
  260. inline rect operator*(const rect& m1,int m2)
  261.    {
  262.      return rect(m1.origin*m2,m1.corner*m2);
  263.  
  264.    }
  265.  
  266.  
  267. inline int operator==(loc a, loc b)
  268.  {
  269.  
  270.   return a.X==b.X && a.Y==b.Y;
  271.  }
  272.  
  273. inline int operator==(const rect& a,const rect& b)
  274.   {
  275.    return a.origin==b.origin && a.corner==b.corner;
  276.   }
  277.  
  278.  
  279. #endif
  280.  
  281.  
  282.